#!/bin/sh
# Build script for the project.

set -e  # stop immediately on error

## prologue: environment defaults
test "$CC" || export CC=cc
test "$CFLAGS" || export CFLAGS="-g -O3"
# some additional flags for safety
export CFLAGS="$CFLAGS -std=c99 -Wall -Wextra -ftrapv -fno-strict-aliasing"

## helpers
# {{{

# noisily signal success if $1 is older than _any_ of the remaining args
older_than() {
  local target="$1"
  shift
  if [ ! -e "$target" ]
  then
    echo "updating $target" >&2
    return 0  # success
  fi
  local f
  for f in "$@"
  do
    if [ "$f" -nt "$target" ]
    then
      echo "updating $target" >&2
      return 0  # success
    fi
  done
  return 1  # failure
}

# noisily switch directories, as a sort of section heading to group older_than blocks
noisy_cd() {
  cd $1
  echo "-- `pwd`" >&2
}
# }}}

## body: things to generate, what they depend on, and how to generate them from their dependencies
older_than a.out x.c && {
  $CC $CFLAGS x.c
}

## epilogue
# if we got to the end, signal success
# otherwise you incorrectly signal failure when the final target is not older_than its dependencies
exit 0
